import random
class Game:
    def __init__(self):
        self.roles = []
        self.room = None

    def start(self):
        while True:
            menu_data = """
                1. 创建角色
                2. 创建房间
                3. 开始游戏
                4. 显示角色
                5. 退出游戏
            """
            print(menu_data)
            choice = input("请输入您的操作：")

            if choice == "1":
                role_name = input("请输入角色名：")
                existing_roles = [role.name for role in self.roles]
                if role_name in existing_roles:
                    print(f"{role_name} 角色已经存在，请不要重复创建！")
                else:
                    self.roles.append(Role(role_name))
                    print(f"角色{role_name}创建成功")

            elif choice == "2":
                if self.room is not None:
                    print("房间已存在，请不要重复创建！")
                else:
                    room_name = input("请输入房间名：")
                    room_no = input("请输入房间编号：")
                    self.room = Room(room_name, room_no)
                    print(f"房间{room_name}创建成功")

            elif choice == "3":
                if len(self.roles) < 2:
                    print("请至少创建两个角色，并加入同一个房间")
                elif self.room is None:
                    print("请先创建房间")
                else:
                    for role in self.roles:
                        # if role.blood
                        self.room.add_member(role)
                    self.room.start_game()

            elif choice == "4":
                if len(self.roles) == 0:
                    print("当前无角色")
                else:
                    print("当前角色有：")
                    for role in self.roles:
                        print(role.name)

            elif choice == "5":
                print("游戏退出！！")
                break

            else:
                print("请输入正确的操作！")


class Room:
    def __init__(self, name, number):
        self.name = name
        self.number = number
        self.members = []
        self.max_members = 2

    def add_member(self, player):
        if len(self.members) >= self.max_members:
            print("房间人数已满！")
        else:
            self.members.append(player)
            print(f"欢迎{player.name}加入{self.name}房间，当前房间人数为{len(self.members)}")
            if len(self.members) == self.max_members:
                print("房间人数已达到最大人数，游戏即将开始！")

    def start_game(self):
        print("游戏开始！")
        player1, player2 = self.members
        while player1.hp > 0 and player2.hp > 0:
            player1.attack(player2)
            player2.attack(player1)
        print("游戏结束！")
        if player1.hp <= 0:
            print(f"{player1.name} 已阵亡！")
            self.members.remove(player1)
        if player2.hp <= 0:
            print(f"{player2.name} 已阵亡！")
            self.members.remove(player2)


class Role:
    def __init__(self, name):
        self.name = name
        self.hp = 100
        self.score = 100

    def attack(self, target):
        if self.hp <= 0:
            print(f"{self.name} 血量不足，无法发起攻击！")
            return
        if target.hp > 0:
            attack_hp = random.randint(10, 30)
            target.hp -= attack_hp
            target.score -= 10
            print(f"{self.name} 对 {target.name} 造成了 {attack_hp} 点伤害")
            print(f"{target.name} 当前积分为：{target.score}，血量为：{target.hp}")
        else:
            print(f"{target.name} 已阵亡！")


game = Game()
game.start()
